package com.atguigu.boot.controller;

import com.atguigu.boot.entities.Customer;
import com.atguigu.boot.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassName customerController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-09-13 22:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/customer")
@Slf4j
@Api("customer接口")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @ApiOperation("新增一个客户")
    @PostMapping("/add")
    public void addCustomer(Customer customer){
        /*Customer customer = new Customer();

        customer.setCname("customer" + new Random().nextInt(30));
        customer.setAge(new Random().nextInt(30) + 1);
        customer.setPhone(getTel());
        customer.setSex((byte) new Random().nextInt(2));
        customer.setBirth(new Date());

        customerService.addCustomer(customer);*/
        customerService.addCustomer(customer);

    }

    //=======================下面是获得一个11位手机号码工具类，懒得写util
    /*private static String[] telFirst=("134,135,136,137,138,139,150,151,152," +
            "157,158,159,130,131,132,155,156,133,153").split(",");
    private static String getTel() {
        int index=getNum(0,telFirst.length-1);
        String first=telFirst[index];
        String second=String.valueOf(getNum(1,888)+10000).substring(1);
        String third=String.valueOf(getNum(1,9100)+10000).substring(1);
        return first+second+third;
    }
    public static int getNum(int start,int end) {
        return (int)(Math.random()*(end-start+1)+start);
    }
*/
    @ApiOperation("按ID查询一个客户")
    @GetMapping("/{id}")
    public Customer getCustomerById(@PathVariable Integer id){
        return customerService.getCustomerById(id);
    }

    @ApiOperation("查询所有客户")
    @GetMapping("/list")
    public List<Customer> list(){
        return customerService.list();
    }

    @ApiOperation("按ID更新客户信息")
    @PutMapping("/{id}")
    public void update(@PathVariable Integer id){
        Customer customer = customerService.getCustomerById(id);

        /*customer.setCname("customer" + new Random().nextInt(30));
        customer.setAge(new Random().nextInt(30) + 1);
        customer.setPhone(getTel());
        customer.setSex((byte) new Random().nextInt(2));
        customer.setBirth(new Date());*/

        customerService.updateCustomer(customer);
    }

    @ApiOperation("按ID删除客户")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id){
        customerService.deleteCustomer(id);
    }
}
